.TH std::has_single_bit 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::has_single_bit \- std::has_single_bit

.SH Synopsis
   Defined in header <bit>
   template< class T >                             \fI(since C++20)\fP
   constexpr bool has_single_bit( T x ) noexcept;

   Checks if x is an integral power of two.

   This overload participates in overload resolution only if T is an unsigned integer
   type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned
   long long, or an extended unsigned integer type).

.SH Parameters

   x - value of unsigned integer type

.SH Return value

   true if x is an integral power of two; otherwise false.

.SH Notes

   Feature-test macro  Value    Std              Feature
   __cpp_lib_int_pow2 202002L (C++20) Integral power-of-2 operations

.SH Possible implementation

.SH First version
   template<std::unsigned_integral T>
       requires !std::same_as<T, bool> && !std::same_as<T, char> &&
                !std::same_as<T, char8_t> && !std::same_as<T, char16_t> &&
                !std::same_as<T, char32_t> && !std::same_as<T, wchar_t>
   constexpr bool has_single_bit(T x) noexcept
   {
       return x && !(x & (x - 1));
   }
.SH Second version
   template<std::unsigned_integral T>
       requires !std::same_as<T, bool> && !std::same_as<T, char> &&
                !std::same_as<T, char8_t> && !std::same_as<T, char16_t> &&
                !std::same_as<T, char32_t> && !std::same_as<T, wchar_t>
   constexpr bool has_single_bit(T x) noexcept
   {
       return std::popcount(x) == 1;
   }

.SH Example


// Run this code

 #include <bit>
 #include <bitset>
 #include <cmath>
 #include <iostream>

 int main()
 {
     for (auto u = 0u; u != 10; ++u)
     {
         std::cout << "u = " << u << " = " << std::bitset<4>(u);
         if (std::has_single_bit(u)) // `ispow2` before P1956R1
             std::cout << " = 2^" << std::log2(u) << " (is power of two)";
         std::cout << '\\n';
     }
 }

.SH Output:

 u = 0 = 0000
 u = 1 = 0001 = 2^0 (is power of two)
 u = 2 = 0010 = 2^1 (is power of two)
 u = 3 = 0011
 u = 4 = 0100 = 2^2 (is power of two)
 u = 5 = 0101
 u = 6 = 0110
 u = 7 = 0111
 u = 8 = 1000 = 2^3 (is power of two)
 u = 9 = 1001

.SH See also

   popcount counts the number of 1 bits in an unsigned integer
   (C++20)  \fI(function template)\fP
   count    returns the number of bits set to true
            \fI(public member function of std::bitset<N>)\fP
   test     accesses specific bit
            \fI(public member function of std::bitset<N>)\fP
